package view.mediator.main
{
	import flash.system.System;
	
	import org.robotlegs.mvcs.StarlingMediator;
	
	import view.component.main.LoadView;
	import view.event.main.LoadViewEvent;
	
	
	/**
	 * 
	 * @author £iang
	 * 
	 */	
	public class LoadViewMediator extends StarlingMediator
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** LoadView界面 */
		[Inject]
		public var loadView:LoadView;
		
		//-----------------------------
		// properties
		//-----------------------------
	
		/** 加载显示信息集合 */
		private var loadInfoTextArr:Array = [ "图集创建完毕...", "数据库连接完毕...",  
			"怪物数据加载完毕...", "武器、防具数据加载完毕...", "xx5加载完毕..." ];
		/** 加载信息的数量 */
		private const infoLen:int = 5;
		/** 当前加载的信息数量 */
		private var loadIter:int = -1;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 注册函数 
		 * 
		 */		
		override public function onRegister():void
		{
			addViewListener( LoadViewEvent.START_LOAD, startLoadHandler );
		    addContextListener( LoadViewEvent.STEP_LOAD_COMPLETE, stepLoadCompleteHandler );
			addContextListener( LoadViewEvent.START_WIKI, startWikiHandler );
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理开始加载数据事件 
		 * @param event 开始加载数据事件 
		 * 
		 */		
		protected function startLoadHandler( event:LoadViewEvent ):void
		{
			dispatch( event );
		}
		
		/**
		 * 处理数据一步步加载完毕后派发的事件 
		 * @param event 数据一步步加载完毕后派发的事件 
		 * 
		 */		
		protected function stepLoadCompleteHandler( event:LoadViewEvent ):void
		{
			loadIter++;
			loadView.setInfoLabelText( loadInfoTextArr[ loadIter ] );
			
			switch(loadIter)
			{
				case 0:
				{
					loadView.playLoadIconAnimation( 0 );
					break;
				}
					
				case 1:
				{
					loadView.playLoadIconAnimation( 1 );
					break;
				}
					
				case 2:
				{
					loadView.playLoadIconAnimation( 2 );
					break;
				}
					
				case 3:
				{
					loadView.playLoadIconAnimation( 3 );
					break;
				}
					
				case 4:
				{
					loadView.playLoadIconAnimation( 4 );
					break;
				}
				
				default:
				{
					break;
				}
			}
		}
		
		private function startWikiHandler( event:LoadViewEvent ):void
		{
			loadView.startWiki();
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 移除函数 
		 * 
		 */		
		override public function onRemove():void
		{
			removeViewListener( LoadViewEvent.START_LOAD, startLoadHandler );
			removeContextListener( LoadViewEvent.STEP_LOAD_COMPLETE, stepLoadCompleteHandler );
			removeContextListener( LoadViewEvent.START_WIKI, startWikiHandler );
			loadInfoTextArr.length = 0;
			loadInfoTextArr = null;
			System.gc();
			super.onRemove();
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function LoadViewMediator()
		{
			super();
		}
	}
}